import Vue from 'vue'
import VueRouter from 'vue-router'
//导入login组件
import login from '@/views/user/login.vue'

Vue.use(VueRouter)
//路由规则
const routes = [
  {
    name: 'login',
    path: '/login',
    component: login
  },
  {
    name: 'edit',
    path: '/edit',
    component: edit
  },
  {
    name: 'layout',
    path: '/',
    component: layout,
    children: [
      {
        name: 'home',
        path: '/home',
        component: home
      },
      {
        name: 'qa',
        path: '/qa',
        component: qa
      },
      {
        name: 'movie',
        path: '/movie',
        component: movie
      },
      {
        name: 'user',
        path: '/user',
        component: user,
        meta: {
          isLogin:true
        }
      },
      {
        path: '/',
        redirect: '/home' // 重定向:重新指向其它path,会改变网址
      },  
    ]
  },
  {
    name: 'notfound',
    path: '/notfound',
    component: notfound
  },
  {
    //*代表任何路径
    path: '*',
    redirect: '/notfound'
  }
]

const router = new VueRouter({
  routes
})
//路由守卫一定要写在创建路由下面
//路由导航守卫
import {getToken} from '@/utils/token.js' 
router.beforeEach((to, from, next) => {
  if (to.meta.isLogin) {
    if (getToken()) {
      next()     
    } else {
      next({ path: '/login' })
      setTimeout(() => {
        console.clear();
      })
    }
  } else {
    next()
  }
})

export default router
